﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Collections.Generic;

namespace ImaginationFactory
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 


    public partial class MainWindow : Window
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// 
        public MainWindow()
        {
            InitializeComponent();

            // 设置窗口大小及位置，使其占满整个用户区域
            this.Width = System.Windows.SystemParameters.WorkArea.Width;
            this.Height = System.Windows.SystemParameters.WorkArea.Height;
            this.Left = System.Windows.SystemParameters.WorkArea.Left;
            this.Top = System.Windows.SystemParameters.WorkArea.Top;

            imageLibrary = new ImageLibrary();
            batchProcessor = new BatchProcessor( imageLibrary );

            ///
            /// 背景特效
            /// 
            ///
            DoubleAnimationUsingKeyFrames animaOpacity = new DoubleAnimationUsingKeyFrames();
            animaOpacity.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.2))));
            animaOpacity.KeyFrames.Add(new LinearDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))));
            //animaOpacity.KeyFrames.Add( new LinearDoubleKeyFrame( 0.5, KeyTime.FromTimeSpan( TimeSpan.FromSeconds( 7 ) ) ) );
            //animaOpacity.KeyFrames.Add( new LinearDoubleKeyFrame( 1, KeyTime.FromTimeSpan( TimeSpan.FromSeconds( 8 ) ) ) );

            this.BeginAnimation(Window.OpacityProperty, animaOpacity);




            ///
            /// 启动新线程打开文件对话框
            /// 
            ///
            //Thread thread = new Thread( OpenFileThread );
            //thread.Start();
            //MyOpenFileDialog();
        }

        #region 对话框

        /// <summary>
        /// 线程函数
        /// </summary>
        public void OpenFileThread()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() { MyOpenFileDialog(); });
        }

        /// <summary>
        /// 打开对话框，选择图片，并设置为VisualBrush中的Image的Source
        /// </summary>
        private void MyOpenFileDialog()
        {
            if ( imageLibrary.MyOpenFileDialog() )
            {
                if ( !isOpen )
                    isOpen = true;

                image.Source = imageLibrary.LoadedImage.Source;

                nameList = batchProcessor.GetFiles();

                ResetTransform();   // 重设变换

                UpdateInfoTextBlock();
            }
        }
        

        #endregion

        #region 鼠标操作

        private bool m_IsMouseLeftButtonDown;
        private Stopwatch sw = new Stopwatch();

        /// <summary>
        /// 左键弹起，释放控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MasterImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContentControl rectangle = sender as ContentControl;

            if (rectangle == null)
            {
                return;
            }

            rectangle.ReleaseMouseCapture();
            m_IsMouseLeftButtonDown = false;
                        
            TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
            Debug.Assert(group != null);
            TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;
                        
            /// 渐渐位移的动画
            DoubleAnimation animationOffsetX = new DoubleAnimation();
            DoubleAnimation animationOffsetY = new DoubleAnimation();

            animationOffsetX.By = ( position.X - m_PreviousMousePoint.X );
            animationOffsetX.AccelerationRatio = 0.5;
            animationOffsetX.DecelerationRatio = 0.4;
            animationOffsetX.Duration = new Duration( TimeSpan.FromSeconds( 0.01 ) );
            animationOffsetX.FillBehavior = FillBehavior.Stop;

            animationOffsetY.By = ( position.Y - m_PreviousMousePoint.Y );
            animationOffsetY.AccelerationRatio = 0.5;
            animationOffsetY.DecelerationRatio = 0.4;
            animationOffsetY.Duration = new Duration( TimeSpan.FromSeconds( 0.01 ) );
            animationOffsetY.FillBehavior = FillBehavior.Stop;
            animationOffsetY.Completed += new EventHandler(animationOffset_Completed);

            offsetTran.BeginAnimation( TranslateTransform.XProperty, animationOffsetX );
            offsetTran.BeginAnimation( TranslateTransform.YProperty, animationOffsetY );
        }

        //
        // 动画完成后设置属性
        //
        void animationOffset_Completed( object sender, EventArgs e )
        {
            TransformGroup group = MainPanel.FindResource( "ImageTransformResource" ) as TransformGroup;
            Debug.Assert( group != null );

            ScaleTransform transform = group.Children[ 0 ] as ScaleTransform;
            TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;

            if ( imageLibrary.Width * ( transform.ScaleX + delta ) > 100 || delta > 0 )
            {
                /// 修正位移
                /// 
                offsetTran.X += position.X - m_PreviousMousePoint.X;
                offsetTran.Y += position.Y - m_PreviousMousePoint.Y;
            }
        }


        private Point m_PreviousMousePoint;     /// 记录鼠标的坐标

        /// <summary>
        /// 鼠标左键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MasterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
            ContentControl rectangle = sender as ContentControl;

            if (rectangle == null)
            {
                return;
            }

            rectangle.CaptureMouse();
            m_IsMouseLeftButtonDown = true;
            m_PreviousMousePoint = e.GetPosition(rectangle);

            // 计时器开始
            //sw.Reset();
            //startPoint = e.GetPosition( MasterImage );
            //sw.Start();    
            
        }

        /// <summary>
        /// 鼠标移动时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MasterImage_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ContentControl rectangle = sender as ContentControl;
            if (rectangle == null)
            {
                return;
            }

            UpdateInfoTextBlock();

            if (m_IsMouseLeftButtonDown)
            {
                DoImageMove(rectangle, e);

                /// 每100毫秒取一次点
                //if ( sw.IsRunning && sw.ElapsedMilliseconds > 50 )
                //{
                //    sw.Reset();
                //    startPoint = e.GetPosition( MasterImage );
                //    sw.Start();
                //}
            }
        }

        private Point position;
        /// <summary>
        /// 移动图片
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="e"></param>
        private void DoImageMove(ContentControl rectangle, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                return;
            }
            
            TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
            Debug.Assert(group != null);
            TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;

            position = e.GetPosition(rectangle);
            offsetTran.X += position.X - m_PreviousMousePoint.X;
            offsetTran.Y += position.Y - m_PreviousMousePoint.Y;
            
            m_PreviousMousePoint = position;
        }

        private double delta;
        Point ptOnImg;

        /// <summary>
        /// 鼠标中键滚动缩放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MasterImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
            Debug.Assert(group != null);

            ScaleTransform transform = group.Children[0] as ScaleTransform;
            TranslateTransform offsetTran = group.Children[1] as TranslateTransform;

            ptOnImg = Mouse.GetPosition(image);
            delta = e.Delta * 0.002 * (transform.ScaleX > 1 ? Math.Pow(2 * transform.ScaleX, 1) : Math.Pow(transform.ScaleX, 0.5));

            #region 非动画放缩            

            //if ( imageLibrary.Width * ( transform.ScaleX + delta ) > 200 || delta > 0 )
            //{
                ///// 修正位移
                ///// 
                //offsetTran.X -= ptOnImg.X * delta;
                //offsetTran.Y -= ptOnImg.Y * delta;

                ///// 进行放缩
                //transform.ScaleX += delta;
                //transform.ScaleY += delta;
            //}
            

            /// Method 2: Error
            /// 
            //double x = transform.ScaleX, y = transform.ScaleY;
            //transform.ScaleX = 1; transform.ScaleY = 1;
            //if ( delta > 0 )
            //{
            //    transform.CenterX = ptOnImg.X;
            //    transform.CenterY = ptOnImg.Y;
            //}
            //transform.ScaleX = x + delta; transform.ScaleY = y + delta;
            #endregion

            #region 动画

            if ( imageLibrary.Width * ( transform.ScaleX + delta ) > 200 || delta > 0 )
            {
                /// 渐渐放大的动画
                DoubleAnimation animationScale = new DoubleAnimation();

                /// 放大的倍率根据已经倍率值来进行修正
                /// 倍率越大每次放大的倍率越大，反之越小。

                animationScale.By = delta;
                animationScale.AccelerationRatio = 0.5;
                animationScale.DecelerationRatio = 0.4;
                animationScale.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
                animationScale.FillBehavior = FillBehavior.Stop;

                transform.BeginAnimation( ScaleTransform.ScaleXProperty, animationScale );
                transform.BeginAnimation( ScaleTransform.ScaleYProperty, animationScale );


                /// 渐渐位移的动画
                DoubleAnimation animationOffsetX = new DoubleAnimation();
                DoubleAnimation animationOffsetY = new DoubleAnimation();

                animationOffsetX.By = -ptOnImg.X * delta;
                animationOffsetX.AccelerationRatio = 0.5;
                animationOffsetX.DecelerationRatio = 0.4;
                animationOffsetX.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
                animationOffsetX.FillBehavior = FillBehavior.Stop;

                animationOffsetY.By = -ptOnImg.Y * delta;
                animationOffsetY.AccelerationRatio = 0.5;
                animationOffsetY.DecelerationRatio = 0.4;
                animationOffsetY.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
                animationOffsetY.FillBehavior = FillBehavior.Stop;
                animationOffsetY.Completed += new EventHandler( animationOffsetY_Completed ); // 设置属性

                offsetTran.BeginAnimation( TranslateTransform.XProperty, animationOffsetX );
                offsetTran.BeginAnimation( TranslateTransform.YProperty, animationOffsetY );
            }

            #endregion

            UpdateInfoTextBlock();
        }

        //
        // 动画完成后设置属性
        //
        void animationOffsetY_Completed( object sender, EventArgs e )
        {
            TransformGroup group = MainPanel.FindResource( "ImageTransformResource" ) as TransformGroup;
            Debug.Assert( group != null );

            ScaleTransform transform = group.Children[ 0 ] as ScaleTransform;
            TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;

            if ( imageLibrary.Width * ( transform.ScaleX + delta ) > 100 || delta > 0 )
            {
                /// 修正位移
                /// 
                offsetTran.X -= ptOnImg.X * delta;
                offsetTran.Y -= ptOnImg.Y * delta;

                /// 进行放缩
                transform.ScaleX += delta;
                transform.ScaleY += delta;
            }
        }


        #endregion

        #region 辅助函数 ———— 显示信息

        private void UpdateInfoTextBlock()
        {
            //TransformGroup group = MainPanel.FindResource( "ImageTransformResource" ) as TransformGroup;
            //Debug.Assert( group != null );
            //ScaleTransform transform = group.Children[ 0 ] as ScaleTransform;
            //TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;

            //Point pt = Mouse.GetPosition( image );
            //Point pointToContent = group.Inverse.Transform( pt );
            //Point originPoint = offsetTran.Inverse.Transform( pt );

            //textBlock.Text = "MasterImage : " + Math.Floor( MasterImage.ActualWidth ).ToString() + ' ' + Math.Floor( MasterImage.ActualHeight ).ToString()
            //    + "\niamge: " + Math.Floor( image.ActualWidth ).ToString() + ' ' + Math.Floor( image.ActualHeight ).ToString()
            //    //+ "\nInversePt = " + Math.Floor( pointToContent.X ).ToString() + ' ' + Math.Floor( pointToContent.Y ).ToString() 
            //    + "\noriginPoint = " + Math.Floor( originPoint.X ).ToString() + ' ' + Math.Floor( originPoint.Y ).ToString()
            //    + "\n放缩中心：" + Math.Floor( transform.CenterX ) + ' ' + Math.Floor( transform.CenterY )
            //    + "\npt = " + Math.Floor( pt.X ).ToString() + ' ' + Math.Floor( pt.Y ).ToString() + '\n'
            //    + "\n放缩: " + Math.Floor( transform.CenterX ).ToString() + ' ' + Math.Floor( transform.CenterY ).ToString()
            //    + "\n位移：" + Math.Floor( offsetTran.X ) + ' ' + Math.Floor( offsetTran.Y )
            //    + "\n" + Math.Floor( ( transform.ScaleX ) * 100 ) + " %\n";
        }

        #endregion

        #region 弹出ListBox

        public static bool isListBoxInitial;

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            if (isListBoxInitial == false)
            {
                Dialogs.MainListBox mlb = new ImaginationFactory.Dialogs.MainListBox(this);
                mlb.Show();
                mlb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                mlb.VerticalAlignment = VerticalAlignment.Bottom;
                mlb.Topmost = true;
                isListBoxInitial = true;
            }
           
        }


        #endregion

        #region 图像处理
        //---------------------------------------------------------------
        // 图像处理库
        //
        private ImageLibrary imageLibrary;
        private BatchProcessor batchProcessor;
        public ImageLibrary ImageLibrary
        {
            get
            {
                return imageLibrary;
            }
            set
            {
                imageLibrary = value;
            }
        }


        //
        // 将 imageLibrary 中的图像装载并显示出来
        //
        //
        public void ShowPicture()
        {
            //MasterImage.Content = imageLibrary.LoadedImage;
        }

        //
        // 反色
        //
        //
        private void InvertColors_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            imageLibrary.InvertColors();

            image.Source = ImageLibrary.LoadedImage.Source;
        }

        //
        // 水平镜像
        //
        //
        private void HorizonMirror_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            imageLibrary.MirrorEffect(Mirror_Sign.Left_Right);

            image.Source = ImageLibrary.LoadedImage.Source;
        }


        //
        // 上下镜像
        //
        //
        private void verticalMirror_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            imageLibrary.MirrorEffect(Mirror_Sign.On_Down);

            image.Source = ImageLibrary.LoadedImage.Source;
        }



        //
        // 单色
        //
        //
        private void BlackAndWhiteEffectButton_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            imageLibrary.SingleColor( BlackAndWhite.Decoloration );
            image.Source = ImageLibrary.LoadedImage.Source;
        }

        //}



        ////
        //// 批处理
        ////
        ////
        //private void BatchEffects(object sender, RoutedEventArgs e)
        //{

        //}

        //
        // 加载小绘图程序
        //
        //
        private void SmallPainter_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.SmallPainter painter = new Dialogs.SmallPainter();
            painter.Show();
        }



        #endregion

        #region 功能按钮

        //
        // 打开图片
        // 
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(OpenFileThread);
            thread.Start();
        }

         //
        // 保存图片
        // 
        private void Save_Click( object sender, RoutedEventArgs e )
        {
            if ( !isOpen )
                return;

           // ImageLibrary.MySaveFileDialog();
            //ImageLibrary.ColorConstrast( 0.8 );
            //this.image.Source = this.ImageLibrary.LoadedImage.Source;

            // Batch
            //batchProcessor.LoadFiles();

            //Parameter param1 = new Parameter();
            //param1.m_Kind = KindOfEffect.Inverse_Color_image;

            ////Parameter param2 = new Parameter();
            ////param2.m_Kind = KindOfEffect.Inverse_Color_image;

            //Parameter param2 = new Parameter();
            //param2.m_Kind = KindOfEffect.Scale_image;
            //param2.Scale_x = 0.5;
            //param2.Scale_y = 0.5;

            //batchProcessor.AddEffects( ref param1 );
            //batchProcessor.AddEffects( ref param2 );

            //batchProcessor.StartProcess( 10 );
            ImageLibrary.Threshold();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
            
        }

        //
        // 取消
        // 
        private void UnDo_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            this.ImageLibrary.Undo();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
        }

        //
        // 重做
        // 
        private void DoAgain_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            this.ImageLibrary.Redo();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
        }

        //
        // 顺时针旋转
        // 
        private void Clock_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            ImageLibrary.RotateImage(90);
            image.Source = ImageLibrary.LoadedImage.Source;
        }

        //
        // 逆时针旋转
        // 
        private void AntiClock_Click(object sender, RoutedEventArgs e)
        {
            if ( !isOpen )
                return;

            ImageLibrary.RotateImage(270);
            image.Source = ImageLibrary.LoadedImage.Source;
        }

        //
        // 下一张图片 
        // 
        private void NextOne_Click( object sender, RoutedEventArgs e )
        {
            try
            {
                if ( !isOpen )
                    return;

                for ( int i = 0; i < nameList.Count; i++ )
                {
                    if ( nameList[ i ] == imageLibrary.Filename )
                    {
                        if ( i + 1 < nameList.Count )
                        {
                            imageLibrary.LoadImage( nameList[ i + 1 ] );        // 加载图片
                            image.Source = imageLibrary.LoadedImage.Source;     // 显示图片  
                            ResetTransform();                                   // 重设变换
                        }
                        break;
                    }
                }
            }
            catch ( Exception er )
            {
                System.Windows.MessageBox.Show( er.ToString() );

            }
        }

        //
        // 上一张图片 
        // 
        private void PreviousOne_Click( object sender, RoutedEventArgs e )
        {
            try
            {
                if ( !isOpen )
                    return;

                for ( int i = 0; i < nameList.Count; i++ )
                {
                    if ( nameList[ i ] == imageLibrary.Filename )
                    {
                        if ( i - 1 >= 0 )
                        {
                            imageLibrary.LoadImage( nameList[ i - 1 ] );        // 加载图片
                            image.Source = imageLibrary.LoadedImage.Source;     // 显示图片
                            ResetTransform();                                   // 重设变换
                        }
                        break;
                    }
                }
            }
            catch ( Exception er )
            {
                System.Windows.MessageBox.Show( er.ToString() );

            }
        }

        //
        // 恢复变换
        //
        public void ResetTransform()
        {
            TransformGroup group = MainPanel.FindResource( "ImageTransformResource" ) as TransformGroup;
            Debug.Assert( group != null );

            ScaleTransform transform = group.Children[ 0 ] as ScaleTransform;
            TranslateTransform offsetTran = group.Children[ 1 ] as TranslateTransform;

            transform.ScaleX = transform.ScaleY = 1;
            offsetTran.X = offsetTran.Y = 0;
        }

        #endregion

        #region 其他

        public bool isImageMove = false;

        List<string> nameList;      // 同目录下文件列表

        private bool isOpen = false;
        public bool IsOpen
        {
            get
            {
                return isOpen;
            }
        }

        /// 点击关闭程序
        private void CloseButton_Click( object sender, RoutedEventArgs e )
        {
            this.Close();
        }

        private void MasterImage_MouseRightButtonDown( object sender, MouseButtonEventArgs e )
        {
            TransformGroup group = MainPanel.FindResource( "ImageTransformResource" ) as TransformGroup;
            Debug.Assert( group != null );
            ScaleTransform transform = group.Children[ 0 ] as ScaleTransform;

            transform.CenterX = 100;
            transform.CenterY = 100;

            transform.ScaleX = 2;
            transform.ScaleY = 2;

            //System.Windows.MessageBox.Show( "Keydown" );
        }

        bool bClose = false;

        private void mWindow_KeyDown( object sender, System.Windows.Input.KeyEventArgs e )
        {
            if ( e.Key == Key.Escape )
            {
                if ( isOpen && !bClose )
                {
                    if ( System.Windows.MessageBox.Show( "您真的需要离开吗？离开前请保存修改的数据，否则数据将会丢失！",
                        "Imagination Factory",
                        System.Windows.MessageBoxButton.YesNo ) 
                              == System.Windows.MessageBoxResult.Yes )
                    {
                        bClose = true;
                        this.Close();
                    }

                }
                else
                {
                    this.Close();
                }
            }
        }

        private void image_ImageFailed( object sender, RoutedEventArgs e )
        {
            this.Close();
        }
        #endregion

        private void openItem_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(this.OpenFileThread);
            thread.Start();

            e.Handled = true;
        }


        private void saveItem_Click(object sender, RoutedEventArgs e)
        {
            if (!isOpen)
                return;

            ImageLibrary.Threshold();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
        }

        private void saveAsItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void unDoItem_Click(object sender, RoutedEventArgs e)
        {
            if (!isOpen)
                return;

            this.ImageLibrary.Undo();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
        }

        private void doAgainItem_Click(object sender, RoutedEventArgs e)
        {
            if (!isOpen)
                return;

            this.ImageLibrary.Redo();
            this.image.Source = this.ImageLibrary.LoadedImage.Source;
        }

        private void sizeItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.ImageSizeDialog imgDlg = new Dialogs.ImageSizeDialog(this);
            imgDlg.Show();
        }

        private void rotateItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.RotateDialog rtDlg = new Dialogs.RotateDialog(this);
            rtDlg.Show();
        }

        private void cutOutItem_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;

            Dialogs.UsefulCutOut cutDlg = new Dialogs.UsefulCutOut(this);
            //Dialogs.CutOutDialog cutDlg = new Dialogs.CutOutDialog( mWindowTemp, this );

            cutDlg.Show();
        }

        private void hmItem_Click(object sender, RoutedEventArgs e)
        {

            ImageLibrary.MirrorEffect(Mirror_Sign.Left_Right);
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void vmItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.MirrorEffect(Mirror_Sign.On_Down);
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void colorPassageItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.ColorPassageDialog cpDlg = new Dialogs.ColorPassageDialog( this);
            cpDlg.Show();
        }

        private void contrastItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.ContrastDialog ctDlg = new Dialogs.ContrastDialog( this);
            ctDlg.Show();
        }

        private void SingleColorItem_Click(object sender, RoutedEventArgs e)
        {

            ImageLibrary.SingleColor(BlackAndWhite.Decoloration);
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void ErosionItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.Erosion();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void DilationItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.Dilation();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void ThresholdItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.Threshold();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void GT_EqualItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.GT_Equal();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void InnerEdgeItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.InnerEdge();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void outterEdgeItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.Dilation();
            image.Source = ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void gaussItem_Click(object sender, RoutedEventArgs e)
        {
           ImageLibrary.GussianBlur(5, 5);
           image.Source =ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void batchItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.BatchProcessDialog bpDlg = new Dialogs.BatchProcessDialog(this);
            bpDlg.Show();
        }

        private void helpItem_Click(object sender, RoutedEventArgs e)
        {
            //TODO

        }

        private void drawItem_Click(object sender, RoutedEventArgs e)
        {
            ImageLibrary.Laplacian();
            image.Source =ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

        private void SmallPainterItem_Click(object sender, RoutedEventArgs e)
        {
            Dialogs.SmallPainter painter = new Dialogs.SmallPainter();
            painter.Show();
        }

        private void InverteColorItem_Click( object sender, RoutedEventArgs e )
        {
            ImageLibrary.InvertColors();
            image.Source =ImageLibrary.LoadedImage.Source;

            e.Handled = true;
        }

    }

}
